package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.*;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /*不仅要新增菜品还要新增口味*/
    @Override
    @Transactional//开始事务管理
    public void save(DishDTO dishDTO) {
        //新增一条菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);
        //获取新增菜品的id,来放在逻辑外键中
        Long id = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(id);
            }
            //新增n条口味信息
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult find(DishPageQueryDTO dishPageQueryDTO) {
        /*Dish dish = new Dish();
        BeanUtils.copyProperties(dishPageQueryDTO, dish);*/
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        List<DishVO> list = dishMapper.select(dishPageQueryDTO);
        Page<DishVO> page = (Page<DishVO>) list;
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断是否有菜品在售卖状态
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断是否有菜品关联了套餐
        List<Long> res = setmealDishMapper.selectSetmealIdsByDishIds(ids);
        if (res != null && !res.isEmpty()) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品及其口味
        /*for (Long id : ids) {
            dishMapper.delete(id);
            dishFlavorMapper.deleteByDishId(id);
        }*/
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByIds(ids);
    }

    @Transactional
    @Override
    public void updateDishAndFlavor(DishDTO dishDTO) {
        //修改菜品的信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);

        //单独获取菜品的id和口味

        Long id = dishDTO.getId();

        //修改口味信息
        //先把原先的口味删除
        dishFlavorMapper.deleteByDishId(id);
        //再插入新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(e -> {
                e.setDishId(id);
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public DishVO selectById(Long id) {
        //查询菜品有关信息
        Dish dish = dishMapper.selectById(id);

        //查询分类名称
        Long categoryId = dish.getCategoryId();
        String categoryName = categoryMapper.selectCategoryName(categoryId);

        //查询口味的有关信息
        List<DishFlavor> list = dishFlavorMapper.selectFlavorInfo(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setCategoryName(categoryName);
        dishVO.setFlavors(list);
        return dishVO;
    }

    @Override
    public void setStatus(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> list(Long categoryId) {
        return dishMapper.list(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<DishVO> dishVOList = dishMapper.list(dish.getCategoryId());
        for (DishVO dishVO : dishVOList) {
            List<DishFlavor> list = dishFlavorMapper.selectFlavorInfo(dishVO.getId());
            dishVO.setFlavors(list);
        }
        return dishVOList;
    }
}
